package com.ldu.exam.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.ldu.exam.model.MenuModel;
import com.ldu.exam.service.MenuService;
import com.ldu.view.View;

@RequestMapping("/menus")
@RestController
public class MenuController 
{
	@Autowired private MenuService service ;
	/**
	 * 查询全部菜单信息(分页)
	 * @param id
	 * @param page
	 * @return
	 */
	@GetMapping(params="pageable")
	public Page<MenuModel> findAll(@PageableDefault(size=20) Pageable page)
	{
		return this.service.findAll( page);
	}
	
	@GetMapping(value="/noPage")
	public Object findAllNoPage() 
	{
		return this.service.findAllNoPage();
	}
	
	/**
	 * 查询全部菜单信息
	 * @param pageable
	 * @return
	 */
	@JsonView(View.Mini.class)
	@GetMapping
	public Iterable<MenuModel> findAll( @RequestParam String userId)
	{
		if(userId.equals("1b6fa0a6075211e98eb2f2801f1b9fd1"))
		{
			List list = this.service.findAllNoPage();
			return list;
		}else {
			return this.service.findAllByRole( userId);
		}
	}
	
	/**
	 * 根据ID查询菜单
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public Object find(@PathVariable String id)
	{
		return this.service.find(id);
	}
	
	/**
	 * 删除菜单
	 * @param id
	 */
	@DeleteMapping("/{id}")
	public Object delete(@PathVariable String id)
	{
		 this.service.delete(id);
		 return ResponseEntity.noContent().build();
	}
	
	/**
	 * 新增菜单
	 * @param model
	 * @return
	 */
	@PostMapping
	public Object save(@RequestBody MenuModel model)
	{
		return this.service.save(model);
	}
	
	/**
	 * 修改菜单信息
	 * @param id
	 * @param model
	 * @return
	 */
	@PutMapping("/{id}") 
	public Object update( @PathVariable String id , @RequestBody MenuModel model)
	{
		return this.service.update( id , model);
	}
	
	
	/**
	 * 根据名称查询菜单
	 * @param name
	 * @param page
	 * @param size
	 * @return
	 */
	@GetMapping(params="name")
	public  Page<MenuModel> findByName( @RequestParam String name, 
										 @RequestParam( required=false, defaultValue="0")  int page,
										 @RequestParam( required=false, defaultValue="20") int size)
	{
		return this.service.findByName( name, page,size) ;
	}
	
	@GetMapping(value="/isExit")
	public Object isExit(@RequestParam String name)
	{
		return this.service.isExit(name);
	}
	
	@GetMapping(params="parentName")
	public  Page<MenuModel> findByParentName( @RequestParam String parentName, 
										 @RequestParam( required=false, defaultValue="0")  int page,
										 @RequestParam( required=false, defaultValue="20") int size)
	{
		return this.service.findByParentName( parentName, page,size) ;
	}
	
	@GetMapping("/all")
	public Page<MenuModel> findByAll(@RequestParam String name, @RequestParam String parentName,
			@RequestParam( required=false, defaultValue="0")  int page,
			 @RequestParam( required=false, defaultValue="20") int size)
	{
		return this.findByAll(name, parentName, page, size);
	}
}

